<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;

/**
 * 舞台管理控制器
 */
class StageController extends AdminController
{

    public function index()
    {
        $tree = M('Seat_map')->field('id,name,cid,type')->select();
        foreach ($tree as $key => $v) {
            $area = M('Seat_area')->where("mid=" . $v['id'])->field('id,name,mark,price,color')->order('id')->select();
            if ($area) {
                $tree[$key]['area'] = $area;
            }
        }
        $this->assign('tree', $tree);
        $this->meta_title = '舞台管理';
        $this->display();
    }

    public function getConcerts($category = 0)
    {

        if ($category) {
            $docs = M('Document')->where("category_id='$category'")->field('id,title')->select();
            $data['docs'] = $docs;
            $data['status'] = 1;
        } else {
            $data['status'] = 0;
            $data['docs'] = null;
        }
        $this->ajaxreturn($data);
    }

    public function addMap()
    {
        $stage = M('Seat_map');
        if (IS_POST) {
            //提交新建表单
            $data['name'] = I('post.name', '');
            $data['cid'] = I('post.cid', '');
            $data['size'] = I('post.size', '');
            $data['front'] = I('post.front', '');
            $data['seats'] = I('post.seats', '_');
            $data['type'] = I('post.type', 1);
            $res = $stage->add($data);
            if ($res) {
                $this->success('新增成功！', U('index'));
            } else {
                $this->error('添加失败！', U('index'));
            }
        } else {
            $this->meta_title = '新增舞台';
            $this->display();
        }
    }

    /* 编辑分类 */
    public function editMap($id = 0)
    {
        $stage = M('Seat_map');
        if (IS_POST) {
            $data['name'] = I('post.name', '');
            $data['cid'] = I('post.cid', '');
            $data['size'] = I('post.size', '');
            $data['front'] = I('post.front', '');
            $data['seats'] = I('post.seats', '_');
            $data['type'] = I('post.type', 1);
            $res = $stage->where("id='$id'")->save($data);
            if ($res) {
                $this->success('修改成功！', U('index'));
            } else {
                $this->error('修改失败！', U('index'));
            }
        } else {
            /* 获取分类信息 */
            $info = $stage->where("id='$id'")->find();
            $concert = M('Document')->where('id=' . $info['cid'])->find();
            $category = M('Category')->where('id=' . $concert['category_id'])->find();
            $this->assign('info', $info);
            $this->assign('concert', $concert);
            $this->assign('category', $category);
            $this->meta_title = '编辑分类';
            $this->display();
        }
    }

    public function delMap($id)
    {
        if (IS_GET) {
            $area = M('seat_area');
            $res = $area->where("mid='$id'")->count();
            if ($res) {
                $this->error('请先删除分区！-1');
            } else {
                $map = M('seat_map');
                $ress = $map->where("id='$id'")->delete();
                if ($ress) {
                    $this->success('删除成功！');
                } else {
                    $this->error('删除失败！-1');
                }
            }
        }
    }

    public function addArea($pid = 0)
    {
        if ($pid) {
            $map = M('Seat_map');
            $area = M('Seat_area');
            if (IS_POST) {
                //提交新建表单
                $seats = I('seats', '');
                if ($seats) {
                    $data1['seats'] = $seats;
                    $data2['mid'] = $pid;
                    $data2['name'] = I('name', '');
                    $data2['mark'] = I('mark', '');
                    $data2['color'] = I('color', '');
                    $data2['price'] = I('price', 0);
                    $map->startTrans();
                    $res1 = $map->where("id='$pid'")->save($data1);
                    $res2 = $area->add($data2);
                    if ($res1 && $res2) {
                        $map->commit();
                        $this->success('新增成功！', U('index'));
                    } else {
                        $map->rollback();
                        $this->error('添加失败！-1', U('index'));
                    }
                } else {
                    $this->error('添加失败！-2', U('index'));
                }

            } else {
                /* 获取分类信息 */
                $stage = $map->where("id='$pid'")->find();
                $aids = $area->where("mid='$pid'")->count();
                $this->assign('info', null);
                $this->assign('stage', $stage);
                $this->assign('aids', $aids);
                $this->display();
            }
        } else {
            $this->error('操作失败！', U('index'));
        }
    }

    public function editArea($id = null, $pid = 0)
    {
        if ($pid) {
            $map = M('Seat_map');
            $area = M('Seat_area');
            if (IS_POST) {
                //提交新建表单
                $seats = I('seats', '');
                if ($seats) {
                    $data1['seats'] = $seats;
                    $data2['mid'] = $pid;
                    $data2['name'] = I('name', '');
                    $data2['mark'] = I('mark', '');
                    $data2['color'] = I('color', '');
                    $data2['price'] = I('price', 0);
                    $map->startTrans();
                    $res1 = true;
                    $map_seats = $map->where("id='$pid'")->field('seats')->find();
                    if ($map_seats['seats'] != $seats) {
                        $res1 = $map->where("id='$pid'")->save($data1);
                    }
                    $res2 = $area->where("id='$id'")->save($data2);
                    if ($res2 == 0) {
                        $res2++;
                    }
                    if ($res1 && $res2) {
                        $map->commit();
                        $this->success('修改成功！', U('index'));
                    } else {
                        $map->rollback();
                        $this->error('修改失败！-1', U('index'));
                    }
                } else {
                    $this->error('添加失败！-2', U('index'));
                }

            } else {
                /* 获取分类信息 */
                $stage = $map->where("id='$pid'")->find();
                $info = $area->where("id='$id'")->find();
                $this->assign('info', $info);
                $this->assign('stage', $stage);
                $this->display();
            }
        } else {
            $this->error('操作失败！', U('index'));
        }
    }

    public function delArea($id = 0)
    {
        if (IS_GET) {
            $area = M('seat_area');
            $res = $area->where("id='$id'")->delete();
            if ($res) {
                $this->success('删除成功！');
            } else {
                $this->error('删除失败！-1');
            }
        }

    }

    public function make($id = 0)
    {
        if ($id){
            $map = M('Seat_map')->where("id='$id'")->find();
            $areas = M('Seat_area')->where("mid='$id'")->select();
            $document = M('Document')->where("id=".$map['cid'])->find();
            $concert = M('DocumentConcert')->where("id=".$map['cid'])->find();
            $info = "";
            $items = "";
            $min_price = doubleval($areas[0]['price']);
            foreach ($areas as $i => $area){
                $price = doubleval($area['price']);
                if ($price < $min_price) {$min_price = $price;}
                $info[$area['mark']]['price'] = $price;
                $info[$area['mark']]['classes'] = $area['color'];
                $info[$area['mark']]['category'] = $area['name'];
                $items.=$area['mark'].'&available&'.$area['name'].',';
            }
            $items.="z&unavailable&已预定";
            $seats = json_encode($info,JSON_UNESCAPED_UNICODE);
            $seats = strtr($seats,"\"","'");

            $data['cid'] = $map['cid'];
            $data['title'] = date($concert['showtime'],"Y.m.d H:i").$document['title'];
            $data['cover_id'] = $document['cover_id'];
            $data['category_id'] = $document['category_id'];
            $data['stage_name'] = $map['name'];
            $data['map'] = $map['seats'];
            $data['map_name'] = $map['front'];
            $data['size'] = $map['size'];
            $data['areas'] = $seats;
            $data['min_price'] = $min_price;
            $data['items'] = $items;
            $data['status'] = 0;
            $data['create_time'] = time();
            $res = M('Concert_lists')->add($data);
            if ($res) {
                $this->success('操作成功！', U('index'));
            } else {
                $this->error('生成失败！-1', U('index'));
            }
        }
    }
}
